package prv.pursuivant.armory.impl;

import static org.junit.Assert.assertEquals;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;

import org.junit.Before;
import org.junit.Test;

import prv.pursuivant.core.Achievement;
import prv.pursuivant.core.ToonAchievements;
import prv.pursuivant.core.impl.AchievementImpl;
import prv.pursuivant.core.impl.ToonAchievementsImpl;
import prv.pursuivant.core.impl.ToonImpl;

public class XStreamCacheTest {

	private XStreamCache stringOnlyCache;
	private Date dateForTest;
	private ToonImpl kwinto;
	private ToonAchievements achievs1111and1112;
	private String xml1111and1112;

	@Before
	public void setUp() throws Exception {
		stringOnlyCache = new XStreamCache(null);
		final DateFormat dateFormat = DateFormat
				.getDateInstance(DateFormat.MEDIUM);
		dateForTest = dateFormat.parse("2009-05-29");
		kwinto = new ToonImpl("Kwinto", 3);
		final Achievement a1111 = new AchievementImpl(kwinto, dateForTest,
				"1111");
		final Achievement a1112 = new AchievementImpl(kwinto, dateForTest,
				"1112");
		HashMap<String, Achievement> map = new HashMap<String, Achievement>(2);
		map.put(a1111.getId(), a1111);
		map.put(a1112.getId(), a1112);
		achievs1111and1112 = new ToonAchievementsImpl(map);
		xml1111and1112 = "<list><achiev id=\"1111\" date=\"2009-05-29\"/><achiev id=\"1112\" date=\"2009-05-29\"/></list>"
				.replaceAll(">\\s++<", "><");
	}

	@Test
	public void testToXML() {
		String toXml = stringOnlyCache.toXML(kwinto, achievs1111and1112);
		toXml = toXml.replaceAll(">\\s++<", "><");
		assertEquals(xml1111and1112, toXml);
	}

	@Test
	public void testFromXML() {
		ToonAchievements fromXml = stringOnlyCache.fromXML(kwinto,
				xml1111and1112);
		assertEquals(achievs1111and1112, fromXml);
	}

}
